local chengxiang = fk.CreateSkill{
  name = "rmt__chengxiang",
}

Fk:loadTranslationTable{
  ["rmt__chengxiang"] = "称象",
  [":rmt__chengxiang"] = "当你受到1点伤害后，你可以亮出牌堆顶四张牌，获得其中任意张数量点数之和不大于13的牌，将其余的牌置入弃牌堆。"..
  "若获得的牌点数之和恰好为13，你下次发动〖称象〗时多亮出一张牌。",

  ["$rmt__chengxiang1"] = "大象，大象，你过来啊。",
  ["$rmt__chengxiang2"] = "那我问你，象重几何？",
}

Fk:addPoxiMethod{
  name = "rmt__chengxiang",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local n = Fk:getCardById(to_select).number
    for _, id in ipairs(data[2]) do
      n = n + Fk:getCardById(id).number
    end
    return n < 14
  end,
  feasible = Util.TrueFunc,
}

chengxiang:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 4
    if player:getMark(chengxiang.name) > 0 then
      num = 5
      room:setPlayerMark(player, chengxiang.name, 0)
    end
    local cards = table.simpleClone(room:getNCards(num))
    if player:hasSkill("rmt__duanti") then
      player:broadcastSkillInvoke("rmt__duanti")
      room:notifySkillInvoked(player, "rmt__duanti", "special")
      cards = {}
      if table.every(room.alive_players, function (p)
        return player.hp <= p.hp
      end) then
        cards = room:getCardsFromPileByRule("peach,analeptic")
      end
      if table.every(room.alive_players, function (p)
        return player.hp >= p.hp
      end) then
        local ids = table.filter(room.draw_pile, function (id)
          return Fk:getCardById(id).is_damage_card or Fk:getCardById(id).sub_type == Card.SubtypeWeapon
        end)
        if #ids > 0 then
          table.insert(cards, table.random(ids))
        end
      end
      for _, id in ipairs(room:getNCards(num + #cards)) do
        if #cards >= num then break end
        if not table.contains(cards, id) then
          table.insert(cards, id)
        end
      end
    end
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, chengxiang.name, nil, true, player)
    local get = room:askToArrangeCards(player, {
      skill_name = chengxiang.name,
      card_map = {cards},
      prompt = "#chengxiang-choose",
      free_arrange = false,
      box_size = 0,
      max_limit = {num, num},
      min_limit = {0, 1},
      poxi_type = "rmt__chengxiang",
      default_choice = {{}, {cards[1]}},
    })[2]
    local n = 0
    for _, id in ipairs(get) do
      n = n + Fk:getCardById(id).number
    end
    if n == 13 then
      room:setPlayerMark(player, chengxiang.name, 1)
    end
    room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, chengxiang.name, nil, true, player)
    room:cleanProcessingArea(cards, chengxiang.name)
  end
})

return chengxiang